package com.wskeee.ec.components.skin
{
	import com.wskeee.ec.ens;
	import com.wskeee.ec.components.Button;
	import com.wskeee.ec.containers.HBox;
	import com.wskeee.ec.containers.LayoutConst;
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.styles.StyleNames;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	use namespace ens;

	public class ButtonSkin extends BaseSkinDrawer
	{
		protected var hbox:HBox;
		protected var icon:Sprite;
		private var iconData:Object;
		protected var iconBM:Bitmap;
		protected var label:TextField;
		
		protected var skinContainer:Sprite;
		protected var upSkin:Object;
		protected var overSkin:Object;
		protected var downSkin:Object;
		
		override public function init(target:EComponent, ui:*):void
		{
			super.init(target,ui);
			initChildren();
		}
		
		protected function initChildren():void
		{
			hbox = new HBox();
			icon = new Sprite();
			iconBM = new Bitmap();
			
			label = new TextField();
			label.multiline = false;
			
			clip.addChild(hbox);
			
			skinContainer = new Sprite();
			clip.addChildAt(skinContainer,0);
		}
		
		//---------------------
		//redraw
		//---------------------
		override public function redraw():void
		{
			upSkin = getSkin("upSkin");
			if(!upSkin)
				upSkin = drawSkin(getStyle("themeColor") as uint);
			
			overSkin = getSkin("overSkin");
			if(!overSkin)
				overSkin = drawSkin(getStyle("themeOverColor") as uint);
			
			downSkin = getSkin("downSkin");
			if(!downSkin)
				downSkin = drawSkin(getStyle("themeSelectedColor") as uint);
		}
		
		/**
		 * 利用 shape 把皮肤画好，设置9切片方便拉伸
		 * @param fillColor
		 * @return 
		 * 
		 */
		private function drawSkin(fillColor:uint):Shape
		{
			var shape:Shape = new Shape();
			var g:Graphics = shape.graphics;
			g.lineStyle(.1,getStyle("borderColor") as uint);
			g.beginFill(fillColor,getStyle("themeFillAlpha") as Number);
			g.drawRect(0,0,100,20);
			g.endFill();
			shape.scale9Grid = new Rectangle(5,5,90,10);
			return shape;
		}
		
		//========================= validate ====================
		override public function validateProperties():void
		{
			super.validateProperties();
			if(iconData!=btn.icon)
			{
				iconData = btn.icon;
				while(icon.numChildren>0)icon.removeChildAt(0);
				if(btn.icon is BitmapData && iconBM.bitmapData!=btn.icon)
				{
					iconBM.bitmapData = btn.icon as BitmapData;
					icon.addChild(iconBM);
				}else if(btn.icon && btn.icon is DisplayObject)
				{
					icon.addChild(btn.icon as DisplayObject);
				}else if(btn.icon && icon is Class)
				{
					icon.addChild(new btn.icon() as DisplayObject);
				}
			}
			
			//设置 label 样式
			var tf:TextFormat = new TextFormat();
			tf.color = getStyle(state == Button.BUTTON_STATE_UP ? "textColor" : 
				state == Button.BUTTON_STATE_OVER ? "textOverColor" : "textSelectedColor");
			if(label.text!=btn.label)
				label.autoSize = TextFieldAutoSize.LEFT;
			label.defaultTextFormat = tf;
			label.text = btn.label;
		}
		
		override public function validateDisplayList():void
		{
			super.validateDisplayList();
			
			hbox.horizontalGap = getStyle("horizontalGap") as Number;
			hbox.verticalAlign = getStyle("verticalAlign") as String;
			hbox.horizontalAlign = getStyle("horizontalAlign") as String;
			hbox.paddingTop = getStyle("paddingTop") as Number;
			hbox.paddingBottom = getStyle("paddingBottom") as Number;
			hbox.paddingLeft = getStyle("paddingLeft") as Number;
			hbox.paddingRight = getStyle("paddingRight") as Number;
			
			var hasIcon:Boolean = btn.icon;
			hbox.removeAllChildren();
			if(hasIcon)hbox.addChild(icon);
			if(btn.label!="")
			{
				if(btn.labelPlacement == LayoutConst.LEFT)
					hbox.addChildAt(label,0);
				else
					hbox.addChild(label);
			}
			hbox.validateNow();
			
			var contentWidth:Number = icon.width+label.width+(hasIcon ? hbox.horizontalGap : 0);		
			const w:Number = !isNaN(btn.explicitWidth) ?  btn.width : contentWidth + (hbox.paddingLeft + hbox.paddingRight);
			const h:Number = btn.height;
			
			//y居中
			//hbox.y = (h - hbox.height)/2;
			
			if(contentWidth>w-(hbox.paddingLeft + hbox.paddingRight))
			{
				//调整label以适应Buttno宽度
				if(label.stage)
				{
					label.autoSize = TextFieldAutoSize.NONE;
					label.width = Math.max(0,w - (hbox.paddingLeft + hbox.paddingRight) - (hasIcon ? icon.width : 0));
					target.toolTip = btn.label;
					hbox.validateNow(true);
				}
			}
			hbox.validateNow();
			hbox.width = w;
			hbox.height = h;
			//hbox.x = (w - hbox.width)/2;
			
			//重置宽度
			if(isNaN(btn.explicitWidth) && btn.width != w)
				target.setActualSize(w,target.height);
			
			//画皮肤
			while(skinContainer.numChildren>0)
				skinContainer.removeChildAt(0);
			var skin:DisplayObject = Button(target).state == Button.BUTTON_STATE_UP ? 
				upSkin as DisplayObject : Button(target).state == Button.BUTTON_STATE_OVER ? overSkin as DisplayObject : 
				downSkin as DisplayObject
			skinContainer.addChild(skin);
			skin.width = target.width;
			skin.height = target.height;
		}
		
		//========================= end =========================
		protected function get clip():Sprite
		{
			return ui as Sprite;
		}
		protected function get state():String
		{
			return Button(target).state;
		}
		private function get btn():Button
		{
			return target as Button;
		}
	}
}